package com.lzx.applib.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Lzx on 16/11/02 0002.
 */

public abstract class SimpleViewHolder<T> extends RecyclerView.ViewHolder {

    public Context context;

    public SimpleViewHolder(View item) {
        super(item);
        context = item.getContext();
        item.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
               SimpleViewHolder.this.onViewAttachedToWindow();
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                SimpleViewHolder.this.onViewDetachedFromWindow();
            }
        });
    }

    public abstract void init(T data, int position);

    public <E> E find(int id) {
        return (E) itemView.findViewById(id);
    }

    public Context getContext() {
        return itemView.getContext();
    }

    public String getString(int resId) {
        return getContext().getString(resId);
    }

    public String getString(int resId, Object... objs) {
        return getContext().getString(resId, objs);
    }

    public int getColor(int resId) {
        return getContext().getResources().getColor(resId);
    }

    public void setVisible(int visible) {
        itemView.setVisibility(visible);
    }

    public int getVisibility() {
        return itemView.getVisibility();
    }

    public void onViewAttachedToWindow() {}

    public void onViewDetachedFromWindow() {}
}